home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Dialectic 1.1 Source / Dialectic ƒ / Dialectic code / dialectic file management.c < prev    next >
Text File  |  1994-02-16  |  5KB  |  186 lines

  1. /**********************************************************************\
  2.  
  3. File:        dialectic file management.c
  4.  
  5. Purpose:    This module handles creating, setting up, and deleting
  6.             temp files, opening and reading the source file, and
  7.             opening and writing the dest file.
  8.  
  9.  
  10. Dialectic -=- dialect text conversion extraordinare
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "Script.h"
  31. #include "program globals.h"
  32. #include "dialectic file management.h"
  33. #include "msg environment.h"
  34. #include "util.h"
  35.  
  36. FSSpec            inputFS, outputFS, tempFS;
  37. Boolean            deleteTheThing;
  38. int                inputRefNum, outputRefNum;
  39. unsigned long    gWhatsReallyInInputBuffer;
  40. unsigned long    gTotalOutputLength;
  41.  
  42. void InitFiles(void)
  43. {
  44.     inputRefNum=outputRefNum=0;
  45.     gWhatsReallyInInputBuffer=gTotalOutputLength=0L;
  46. }
  47.  
  48. int OpenInputFile(void)
  49. {
  50.     OSErr            theError;
  51.     
  52.     if (gHasFSSpecs)
  53.         theError=FSpOpenDF(&inputFS, fsRdPerm, &inputRefNum);
  54.     else
  55.         theError=HOpen(inputFS.vRefNum, inputFS.parID, inputFS.name, fsRdPerm, &inputRefNum);
  56.     
  57.     if (theError==noErr)
  58.         GetEOF(inputRefNum, &gInputLength);
  59.     
  60.     return (theError==noErr) ? allsWell : kCantOpenInputFile;
  61. }
  62.  
  63. int CreateTempFile(void)
  64. {
  65.     int                    i;
  66.     Str255                timeStr;
  67.     OSErr                isHuman;
  68.     unsigned long        fileType;
  69.     int                    resultCode;
  70.     
  71.     tempFS=outputFS;
  72.     tempFS.name[0]=0x0a;
  73.     tempFS.name[1]='D';
  74.     tempFS.name[2]='i';
  75.     tempFS.name[3]='a';
  76.     tempFS.name[4]='l';
  77.     tempFS.name[5]='e';
  78.     tempFS.name[6]='c';
  79.     tempFS.name[7]='t';
  80.     tempFS.name[8]='i';
  81.     tempFS.name[9]='c';
  82.     tempFS.name[10]='.';
  83.     NumToString(Time&0x7fffffff, timeStr);
  84.     for (i=1; i<=timeStr[0]; i++)
  85.         tempFS.name[++(tempFS.name[0])]=timeStr[i];
  86.     
  87.     if (gHasFSSpecs)
  88.         isHuman=FSpCreate(&tempFS, CREATOR, 'TeMp', smSystemScript);
  89.     else
  90.         isHuman=HCreate(tempFS.vRefNum, tempFS.parID, tempFS.name, CREATOR, 'TeMp');
  91.     
  92.     return (isHuman==noErr) ? allsWell : kCantCreateTempFile;
  93. }
  94.  
  95. int SetupTempFile(void)
  96. {
  97.     OSErr                isHuman;
  98.     unsigned long        chefLen;
  99.     
  100.     if (gHasFSSpecs)
  101.         isHuman=FSpOpenDF(&tempFS, fsRdWrPerm, &outputRefNum);
  102.     else
  103.         isHuman=HOpen(tempFS.vRefNum, tempFS.parID, tempFS.name, fsRdWrPerm, &outputRefNum);
  104.     
  105.     if (isHuman!=noErr)
  106.         return kCantOpenTempFile;
  107.         
  108.     SetEOF(outputRefNum, 0L);
  109.     SetFPos(outputRefNum, 1, 0L);
  110.  
  111.     return allsWell;
  112. }
  113.  
  114. void FinalizeFiles(Boolean good)
  115. {
  116.     FInfo                theInfo;
  117.     
  118.     if (inputRefNum)
  119.         FSClose(inputRefNum);
  120.     if (outputRefNum)
  121.     {
  122.         SetEOF(outputRefNum, gTotalOutputLength);
  123.         FSClose(outputRefNum);
  124.     }
  125.     FlushVol(0L, outputRefNum);
  126.     
  127.     if (!good)
  128.     {
  129.         if(gHasFSSpecs)
  130.             FSpDelete(&tempFS);
  131.         else
  132.             HDelete(tempFS.vRefNum, tempFS.parID, tempFS.name);
  133.         
  134.         return;
  135.     }
  136.     
  137.     if (deleteTheThing)
  138.     {
  139.         if (gHasFSSpecs)
  140.             FSpDelete(&outputFS);
  141.         else
  142.             HDelete(outputFS.vRefNum, outputFS.parID, outputFS.name);
  143.     }
  144.     FlushVol(0L, outputRefNum);
  145.         
  146.     if (gHasFSSpecs)
  147.         FSpGetFInfo(&inputFS, &theInfo);
  148.     else
  149.         HGetFInfo(inputFS.vRefNum, inputFS.parID, inputFS.name, &theInfo);
  150.  
  151.     theInfo.fdFlags&=~0x0100;    /* clear Inited bit */
  152.     
  153.     if (gHasFSSpecs)
  154.         FSpSetFInfo(&tempFS, &theInfo);
  155.     else
  156.         HSetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name, &theInfo);
  157.     FlushVol(0L, outputRefNum);
  158.     
  159.     if (gHasFSSpecs)
  160.         FSpRename(&tempFS, outputFS.name);
  161.     else
  162.         HRename(tempFS.vRefNum, tempFS.parID, tempFS.name, outputFS.name);
  163.     FlushVol(0L, outputRefNum);
  164. }
  165.  
  166. int ReadInputFile(int thisFile, unsigned char* buffer, unsigned long count)
  167. {
  168.     OSErr            isHuman;
  169.     unsigned long    temp;
  170.     
  171.     temp=count;
  172.     isHuman=FSRead(thisFile, &temp, buffer);
  173.     return ((isHuman==eofErr) || (isHuman==noErr)) ? allsWell : kCantReadInputFile;
  174. }
  175.  
  176. int WriteTempFile(int thatFile, unsigned char* buffer, unsigned long theLength)
  177. {
  178.     unsigned long    oldFPos;
  179.     
  180.     gTotalOutputLength+=theLength;
  181.     GetFPos(thatFile, &oldFPos);
  182.     SetEOF(thatFile, gTotalOutputLength);
  183.     SetFPos(thatFile, 1, oldFPos);
  184.     return (FSWrite(thatFile, &theLength, buffer)==noErr ? allsWell : kCantWriteTempFile);
  185. }
  186.